﻿using System;
using System.Web.UI.WebControls;
using Cvk.Staffs;

namespace BatDongSan.Manager
{
    public partial class ManStaffGroupContactAdd : System.Web.UI.Page
    {
        public static int _staffId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _staffId = Convert.ToInt32(Request.QueryString["staffid"]);
                if (_staffId > 0)
                    BindGroupContact(_staffId);
            }
        }
        protected void BindGroupContact(int staffId)
        {
            try
            {
                ckblGroupContact.DataSource = StaffBll.ContactGroupStaff.GetList(Utility.strConnect);
                ckblGroupContact.DataTextField = "GroupName";
                ckblGroupContact.DataValueField = "GroupId";
                ckblGroupContact.DataBind();

                foreach (ListItem li in ckblGroupContact.Items)
                {
                    Cvk_Contact_GroupStaff info = new Cvk_Contact_GroupStaff();
                    info.GroupId = Convert.ToInt32(li.Value);
                    info.StaffId = staffId;
                    li.Selected = StaffBll.ContactGroupStaff.Check(info, Utility.strConnect);
                }
            }
            catch { }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListItem li in ckblGroupContact.Items)
                {
                    Cvk_Contact_GroupStaff info = new Cvk_Contact_GroupStaff();
                    info.StaffId = _staffId;
                    info.GroupId = Convert.ToInt32(li.Value);
                    if (li.Selected)
                        StaffBll.ContactGroupStaff.Insert(info, Utility.strConnect);
                    else
                        StaffBll.ContactGroupStaff.Delete(info, Utility.strConnect);
                }
                BindGroupContact(_staffId);
                lbStatus.Text = "Cập nhật thành công";
            }
            catch { }
        }
    }
}
